//
//  ConVipViewController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/8/3.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConVipViewController.h"

#import "ConMyTeamController.h"
#import "ConMyClubEditViewController.h"
#import "ConGoodsManagerController.h"
#import "ConIntegralController.h"
#import "PersonNetWork.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import "SZLTabBarViewController.h"
#import "NewPersonalNetWork.h"
#import <MJRefresh.h>
#import "ShareTool.h"
#import "ShareContentModel.h"
#import "CommendImageTextModel.h"
#import "CommendCollectionCell.h"

@interface ConVipViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic,strong) UICollectionView *collection;
@property (nonatomic,assign) NSInteger page;//页码
@property (nonatomic,assign) BOOL noDate;//数据不够20 不必去请求了
@property (nonatomic,strong) NSMutableArray *sources;

@property (nonatomic,weak) UILabel *dayLab;
@property (nonatomic,weak) UILabel *yearLab;
@property (nonatomic,weak) UILabel *subLab;

@end

@implementation ConVipViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.page = 1;
    [self setHeadView];
    [self.view addSubview:self.collection];
//    self.collection.transform = CGAffineTransformMakeRotation(M_PI);
    [self getCommendList];
}
//顶部naviBar
- (void)setHeadView
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW-20, 44)];
    self.navigationItem.titleView = view;
    
//    UIPickerView *picker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, 40, 60)];
//    picker.dataSource = self;
//    picker.delegate = self;
//    [view addSubview:picker];
    
    UILabel *dayLab = [[UILabel alloc]initWithFrame:CGRectMake(10, view.height-24-5, 35, 24)];
    dayLab.font = Font(25);
    self.dayLab = dayLab;
    [view addSubview:dayLab];
    
    UILabel *yearLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(dayLab.frame), view.height-14-5, 70, 14)];
    yearLab.font = Font(14);
    self.yearLab = yearLab;
    [view addSubview:yearLab];
    
    UILabel *subLab = [[UILabel alloc]initWithFrame:CGRectMake(view.width-130, view.height-14-5, 120, 14)];
    subLab.font = Font(12);
    subLab.textColor = [UIColor grayColor];
    subLab.textAlignment = NSTextAlignmentRight;
    self.subLab = subLab;
    [view addSubview:subLab];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return @"22";
}
#pragma mark - Collcction view datasourece delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.sources.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CommendCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([CommendCollectionCell class]) forIndexPath:indexPath];
    cell.model = self.sources[indexPath.item];
//    cell.transform = CGAffineTransformMakeRotation(-M_PI);
    __WEAK
    cell.zanClickBlock = ^(CommendModel *model, UIButton *btn) {
        [weakSelf zanwithModel:model btn:btn];
    };
    cell.shareBlock = ^(CommendModel *model) {
        [weakSelf shareWithModel:model];
    };
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self configHeader:self.sources[indexPath.item]];
    if ((indexPath.item == self.sources.count-1)&&!self.noDate) {
        self.page++;
        [self getCommendList];
    }
}

- (void)shareWithModel:(CommendModel *)model
{
    ShareContentModel *shareModel = [[ShareContentModel alloc]init];
    shareModel.title = model.personName;
    shareModel.content = [NSString stringWithFormat:@"%@ 邀请您点赞",model.personIntroduce];
    CommendImageTextModel *textModel = model.atlasList[0];
    shareModel.sharePicUrl = textModel.image;
    shareModel.pageUrl = model.shareUrl;
    [ShareTool share:shareModel];
}
#pragma mark ---------获取推荐列表--------------------
- (void)getCommendList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"pageNo"] = @(self.page);
    if ([SZLAccountHelper isAlreadyLogin]) {
        parame[@"token"] = [SZLAccountHelper shareInstance].token;
    }
    __WEAK
    [self showWaiter];
    [NewPersonalNetWork getPersonConnectionListWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        [weakSelf hideWaiter];
        if (success) {
            if (self.page == 1&&lists.count == 0) {
                [self showNoDataLab:self.view range:CGRectMake(0, self.view.height*0.5-20, ScreenW, 20) title:@"暂无数据"];
                return ;
            }
            lists.count == 0?self.page--:self.page;
            if (lists.count <= 20) {
                self.noDate = YES;
            }
            [self.sources addObjectsFromArray:lists];
            [self.collection reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}

//顶部赋值
- (void)configHeader:(CommendModel *)model
{
    self.dayLab.text = model.day;
    self.yearLab.text = model.yearMonth;
    self.subLab.text = model.title;
}
//点赞请求
- (void)zanwithModel:(CommendModel *)model btn:(UIButton *)btn
{
    if ([SZLAccountHelper shouldLogin]) return;
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"personId"] = @(model.ID);
    if ([SZLAccountHelper isAlreadyLogin]) {
        parame[@"token"] = [SZLAccountHelper shareInstance].token;
    }
    [self showWaiter];
    [NewPersonalNetWork zanClickParame:parame response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            if (btn.isSelected) {//取消
                btn.selected = NO;
                [btn setTitle:[NSString stringWithFormat:@"%zd",[btn.titleLabel.text integerValue]-1] forState:UIControlStateNormal];
            }else {
                btn.selected = YES;
                [btn setTitle:[NSString stringWithFormat:@"%zd",[btn.titleLabel.text integerValue]+1] forState:UIControlStateNormal];
            }
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}

- (UICollectionView *)collection
{
    if (!_collection) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.itemSize = CGSizeMake(ScreenW, ScreenH-SafeAreaTopHeight-SafeAreaBottomHeight);
        flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flow.minimumLineSpacing = 0;
        flow.minimumInteritemSpacing = 0;
        _collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenW,ScreenH-SafeAreaTopHeight-SafeAreaBottomHeight) collectionViewLayout:flow];
        _collection.dataSource = self;
        _collection.delegate = self;
        _collection.backgroundColor = [UIColor whiteColor];
        _collection.pagingEnabled = YES;
        _collection.showsVerticalScrollIndicator = NO;
        _collection.showsHorizontalScrollIndicator = NO;
        [_collection registerClass:[CommendCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([CommendCollectionCell class])];
    }
    return _collection;
}
- (NSMutableArray *)sources
{
    if (!_sources) {
        _sources = [NSMutableArray array];
    }
    return _sources;
}

@end
